<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <!-- <meta name="viewport"
           content="width=device-width, role-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">-->
    <title>layui在线调试</title>
    <!--<link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css?t=1542630986927" media="all">-->
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <script src="/plugins/layui/layui.js"></script>
    <script src="/plugins/jquery/jquery.min.js?v=1.12.4"></script>
    <script src="/common/js/my-jquery.js?v=1.12.4"></script>

    <script src="/common/js/common.js?v=1.12.4"></script>

    <script src="/common/js/page.js?v=1.12.4"></script>
    <style>
        body {
            margin: 10px;
        }

        .demo-carousel {
            height: 200px;
            line-height: 200px;
            text-align: center;
        }
    </style>
</head>
<body>
<form class="layui-form" action="" id="searchForm">
    <div class="layui-row">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="tel" name="code" autocomplete="off" class="layui-input" placeholder="编号">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="tel" name="name" autocomplete="off" class="layui-input" placeholder="名称">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-normal" type="button" id="searchButton">搜索</button>
        </div>
    </div>
</form>
<table class="layui-hide" id="table-list" lay-filter="table-bar"></table>
<!-- 头部工具栏模板： -->
<script type="text/html" id="head-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="edit">编辑</button>
    </div>
</script>=
<!-- 行工具栏模板 table 中操作按钮-->
<script type="text/html" id="oper-col">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="roleMenu">角色菜单管理</a>
</script>

<blockquote class="layui-elem-quote layui-quote-nm layui-hide" id="footer">layui {{ layui.v }} 提供强力驱动</blockquote>


<script>
    layui.use(['laydate', 'layer', 'table', 'carousel', 'upload', 'form'], function () {
        var laydate = layui.laydate //日期
            , table = layui.table //表格
            , form = layui.form;//表单
        var roletTable = null;

        //执行一个 table 实例
        roletTable = table.render({
            elem: '#table-list'
            , height: 720
            , url: '/role/list.json' //数据接口
            //, where: $("#form").serializeJson() //参数
            , response: {statusCode: 200}//200为成功状态码
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
            , request: {
                pageName: 'current' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , title: '用户表'
            , page: true //开启分页
            , toolbar: '#head-toolbar' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , limit: 10
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'code', title: '编号', width: 120}
                , {field: 'name', title: '名称', width: 120, sort: true, totalRow: true}
                , {templet: '#oper-col', title: '操作'}
            ]]
        });
        //初始化搜索
        Page.initSearchForm($("#searchButton"), $("#searchForm"), roletTable, form);
        //监听头工具栏事件
        table.on('toolbar(table-bar)', function (obj) { ///注：toolbar 是工具条事件名，table-bar 是 table 原始容器的属性 lay-filter="对应的值"
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    var url = "/role/add.html";
                    Common.topOpenDlg(url, "角色管理>新增", '25%', '42%');
                    break;
                case 'edit':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        var url = "/role/edit.html?id=" + data[0].id;
                        Common.topOpenDlg(url, "角色管管理>新增", '25%', '42%');
                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        Common.deleteData("/role/delete.json", {id: data[0].id}, '/role/index.html');
                    }
                    break;
            }
        });
        //监听行工具事件
        table.on('tool(table-bar)', function (obj) { //注：tool 是工具条事件名，table-bar 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'add') {
                var url = "/role/add.html?parentId=" + data.id;
                Common.topOpenDlg(url, "角色管管理>新增", '27%', '52%');
            } else if (layEvent === 'edit') {
                var url = "/role/edit.html?id=" + data.id;
                Common.topOpenDlg(url, "角色管管理>编辑", '25%', '44%');
            } else if (layEvent === 'del') {
                Common.deleteData("/role/delete.json", {id: data.id}, '/role/index.html');
            }else if (layEvent === 'roleMenu') {
                var url = "/roleMenu/roleMenu.html?roleId=" + data.id;
                Common.topOpenDlg(url, "角色管理>角色菜单管理", '400px', '400px');
            }
        });
    });
</script>
</body>
</html>
